package demo;

import service.serviceimpl.ServiceUserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author wuchao66
 * @Date 2023/5/23 10:25
 * @PackageName:${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
@WebServlet(name = "ServletDemo",urlPatterns = "/ServletDemo")
public class ServletDemo extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        HttpSession session = request.getSession();
        session.setAttribute("user",name);
        session.setMaxInactiveInterval(3600);
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        int load = serviceUser.load("select count(1) from userinfo where name=? and dept=?", name, pwd);
        System.out.println(load);
        if(load>0){
            request.getRequestDispatcher("/t163demo/t163.jsp").forward(request,response);
        }else{
            request.setAttribute("load","账户或者密码错误");
           // request.getRequestDispatcher("/t163demo/load.jsp").forward(request,response);
            response.sendRedirect(request.getContextPath()+"/t163demo/load.jsp?load=y");
        }

        out.flush();
        out.close();
    }





    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        int load = serviceUser.load("select count(1) from userinfo where name=? and dept=?", name, pwd);
        System.out.println(load);
        if(load>0){
            request.getRequestDispatcher("/t163demo/t163.jsp").forward(request,response);
        }else{
            request.setAttribute("load","账户或者密码错误");
            request.getRequestDispatcher("/t163demo/load.jsp").forward(request,response);
            //response.sendRedirect(request.getContextPath()+"/t163demo/t163.jsp");
        }
        out.flush();
        out.close();
    }


}
